package com.collect.cameroon.zhiScene.zhiWidget.zhiChartView;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

import com.collect.cameroon.R;
import com.collect.cameroon.zhiAdater.zhiChartLevelDay.ZHIChartLevelDayAdjuster;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRLevelDayEntity;

import java.util.ArrayList;

public class ZHIChartLevelDay extends RelativeLayout {
    private Context mContext;
    private View mView;
    private View mLine0, mLine1, mLine2, mLine3;
    private RecyclerView mRecyclerView;

    private ZHIChartLevelDayAdjuster mZHIChartLevelDayAdjuster;

    public ZHIChartLevelDay(Context context) {
        this(context, null);
    }

    public ZHIChartLevelDay(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHIChartLevelDay(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.mContext = context;

        initView();
    }

    @SuppressLint("NotifyDataSetChanged")
    private void initView() {
        mView = View.inflate(mContext, R.layout.view_chart_level_day, this);
        mLine0 = mView.findViewById(R.id.view_line_0);
        mLine1 = mView.findViewById(R.id.view_line_1);
        mLine2 = mView.findViewById(R.id.view_line_2);
        mLine3 = mView.findViewById(R.id.view_line_3);
        mRecyclerView = mView.findViewById(R.id.rc_list);

        mZHIChartLevelDayAdjuster = new ZHIChartLevelDayAdjuster();
        mZHIChartLevelDayAdjuster.setOnZHIChartLevelDayAdjusterItemClickListener(zhirLevelDayEntity -> {
            if (zhirLevelDayEntity != null) {
                zhirLevelDayEntity.setChecked(true);
                mZHIChartLevelDayAdjuster.notifyDataSetChanged();
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        mRecyclerView.setAdapter(mZHIChartLevelDayAdjuster);
    }

    public void setDataSources(ArrayList<ZHIRLevelDayEntity> arrayList) {
        if (arrayList == null) {
            arrayList = new ArrayList<>();
        }

        int totalWidth = mView.getWidth();
        int sigleWidth = totalWidth/30;

        for(int i=0;i<arrayList.size();i++){
            ZHIRLevelDayEntity zhirLevelDayEntity = arrayList.get(i);
            zhirLevelDayEntity.setItemWidth(sigleWidth);
        }

        mZHIChartLevelDayAdjuster.setNewData(arrayList);
    }
}
